
package com.ifcar.app.adapter;

import com.ifcar.app.model.Comment;
import com.ifcar.app.model.MyComment;
import com.ifcar.app.model.ReplyMe;
import com.ifcar.app.util.ModelUtils;
import com.ifcar.app.activity.R;

import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class ReplyMeAdapter extends CommentAdapter {
    public ReplyMeAdapter(Context context, List<Map<String, Object>> objects) {
        super(context, objects);
    }
    
    String getOrig(Map<String, Object> data, Map<String, Object> last) {
        String pi = (String) ModelUtils.getValue(last, Comment.PARAM_PI);
        return (!TextUtils.isEmpty(pi) && pi.startsWith("PHOT") ? getContext().getText(
                R.string.comment_orig_picture).toString() : getContext().getText(
                R.string.comment_orig).toString())
                + (String) ModelUtils.getValue(data, Comment.PARAM_S);
    }

    @Override
    public int getLastFloorNum(Map<String, Object> data) {
//        String lv = (String) ModelUtils.getValue((Map<String, Object>) data, ReplyMe.PARAM_LV);
//        if (TextUtils.isEmpty(lv)) {
//            return -1;
//        }
//
//        try {
//            return Integer.valueOf(lv);
//        } catch (Exception e) {
//            return -1;
//        }
        
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);
        if (map == null || map.size() == 0) {
            return -1;
        }

        try {
            Object[] keys = map.keySet().toArray();
            Arrays.sort(keys);
            return Integer.valueOf((String)keys[keys.length - 1]);
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }

    }

    @Override
    public int getFloorCount(Object data) {
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);
        if (map == null || map.size() == 0) {
            return 0;
        }

        int hide = getHidePosition((Map<String, Object>) data);
        if (hide >= 0) {
            return 4;
        }

        // 最后一层已经已经绑定过
        return map.size() - 1;
    }

    @Override
    int getHidePosition(Map<String, Object> data) {
        int last = getLastFloorNum(data);
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);
        if (map == null || map.size() == 0) {
            return -1;
        }
        // 当最后一层标志和bd的size不一致时,有隐藏
        return last > map.size() ? 2 : -1;
    }
    
    @Override
    void expandHide(final Map<String, Object> data) {
        if (data == null) {
            return;
        }

        boolean isExpanding = isExpanding(data);
        if (isExpanding) {
            return;
        }

        int last = getLastFloorNum(data);
        if (last < 1) {
            return;
        }
        
        final String boardid = (String)ModelUtils.getValue(data, ReplyMe.PARAM_BI);
        if (TextUtils.isEmpty(boardid)) {
            return;
        }
        
        Map<String, Object> bdList = (Map<String, Object>) ModelUtils.getValue(
                (Map<String, Object>) data, MyComment.PARAM_BD);

        // 取最后一层的pi,拆开得到p和d
        Map<String, Object> map = (Map<String, Object>) ModelUtils.getValue(bdList, String
                .valueOf(last));

        final String pi = (String) ModelUtils.getValue(map, Comment.PARAM_PI);
        if (TextUtils.isEmpty(pi)) {
            return;
        }

        String[] ss = pi.split("_");
        if (ss == null || ss.length != 2) {
            return;
        }

        final String d = ss[0];
        if (TextUtils.isEmpty(d)) {
            return;
        }

        final String p = ss[1];
        if (TextUtils.isEmpty(p)) {
            return;
        }

        // 标识当前楼正在展开
        data.put(Comment.PARAM_EXPANDING, true);
        new Thread() {
            @Override
            public void run() {
                // try {
                // Thread.sleep(3000);
                // } catch (InterruptedException e) {
                // // TODO Auto-generated catch block
                // e.printStackTrace();
                // }
                Map<String, Object> result = new HashMap<String, Object>();
                result.put("data", data);
                result.put("bd", Comment.expand(boardid, d, p, getContext()));
                Message msg = mHandler.obtainMessage(MSG_EXPANDS_HIDE);
                msg.obj = result;
                mHandler.sendMessage(msg);
            }

        }.start();
        notifyDataSetChanged();
    }
    
    void handleMessage(Message msg) {
        switch (msg.what) {
            case MSG_EXPANDS_HIDE:
                Map<String, Object> result = (Map<String, Object>) msg.obj;
                if (result == null) {
                    return;
                }

                Map<String, Object> data = (Map<String, Object>) ModelUtils
                        .getValue(result, "data");
                if (data == null) {
                    return;
                }

                boolean isExpanding = isExpanding(data);
                if (!isExpanding) {
                    return;
                }

                Map<String, Object> bd = (Map<String, Object>) ModelUtils.getValue(result, "bd");

                if (bd != null && bd.size() > 0) {
                    data.put(ReplyMe.PARAM_BD, bd);
                } else {
                    Toast.makeText(getContext(), "展开跟帖失败", Toast.LENGTH_SHORT).show();
                }

                // 重置展开标识
                data.remove(Comment.PARAM_EXPANDING);
                
                notifyDataSetChanged();
                break;

            default:
                break;
        }
    }
}
